package cn.xiaomagege.platform.util;

import cn.xiaomagege.platform.module.sys.entity.SysMenu;
import cn.xiaomagege.platform.module.sys.entity.SysRole;
import cn.xiaomagege.platform.vo.TreeSelect;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TreeSelectUtil {
    
    public List<TreeSelect> MenuSelectTree(List<SysMenu> menu) {
        List<TreeSelect> list = new ArrayList<>();
        for (SysMenu x : menu) {
            TreeSelect ts = new TreeSelect();
            if("-1".equals(x.getParentId())) {
                ts.setId(x.getId());
                ts.setName(x.getName());
                ts.setChildren(MenuSelectChild(menu, x.getId()));
                list.add(ts);
            }
        }
        Collections.reverse(list);
        return list;
    }
    
    public List<TreeSelect> MenuSelectChild(List<SysMenu> menu, String id) {
        List<TreeSelect> lists = new ArrayList<>();
        for(SysMenu x : menu) {
            TreeSelect ts = new TreeSelect();
            if(id.equals(x.getParentId())) {
                ts.setId(x.getId());
                ts.setName(x.getName());
                ts.setChildren(MenuSelectChild(menu, x.getId()));
                lists.add(ts);
            }
        }
        return lists;
    }
    
    public List<TreeSelect> RoleSelectTree(List<SysRole> role) {
        List<TreeSelect> list = new ArrayList<>();
        for (SysRole x : role) {
            TreeSelect ts = new TreeSelect();
            if("-1".equals(x.getParentId())) {
                ts.setId(x.getId());
                ts.setName(x.getName());
                ts.setChildren(RoleSelectChild(role, x.getId()));
                list.add(ts);
            }
        }
        Collections.reverse(list);
        return list;
    }
    
    public List<TreeSelect> RoleSelectChild(List<SysRole> role, String id) {
        List<TreeSelect> lists = new ArrayList<>();
        for(SysRole x : role) {
            TreeSelect ts = new TreeSelect();
            if(id.equals(x.getParentId())) {
                ts.setId(x.getId());
                ts.setName(x.getName());
                ts.setChildren(RoleSelectChild(role, x.getId()));
                lists.add(ts);
            }
        }
        return lists;
    }
}